const request = require('../../../utils/request.js')
const util = require('../../../utils/util.js')
const country = require('../../../utils/countryCode.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    backpackDetails: {},
    backpackId: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var backpackId = parseInt(options.backpackId);
    that.setData({
      backpackId: backpackId
    })
    var countryName = null;
    var startTime = null;
    var endTime = null;
    var that = this;
    var url = request.URL + '/backpack/byId';
    request.request({
      url: url,
      method: 'GET',
      data: {
        backpackId: backpackId
      },
      success: function(res) {
        console.log(res);
        var backpackDetails = res.data;
        var startTime = util.formatTime(parseInt(backpackDetails.startTime));
        var endTime = util.formatTime(parseInt(backpackDetails.endTime));
        startTime = startTime.split(' ')[0];
        endTime = endTime.split(' ')[0];
        console.log(startTime)
        var countryCode = backpackDetails.countryId;
        var countryInfo = country.getCountryInfo(countryCode);
        var countryName = countryInfo.chineseName;
        backpackDetails.countryName = countryName;
        backpackDetails.startTime = startTime;
        backpackDetails.endTime = endTime;
        that.setData({
          backpackDetails: backpackDetails
        })
        console.log(backpackDetails)
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  //转发小程序
  onShareAppMessage: function() {
    var that = this;
    var backpackId = that.data.backpackId;
    return {
      title: '背包详情',
      path: './backpack_detail/backpack_detail?backpackId=' + backpackId,
      success: function(res) {
        //转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success'
        })
      },
      fail: function(res) {
        //转发失败
      }
    }
  },
})